<template>
  <div class="goods-relevant">
    <div class="header">
      <i class="icon" />
      <span class="title">同类商品推荐</span>
    </div>
    <!-- 此处使用改造后的xtx-carousel.vue -->
    <!-- 使用前：
    解决数据问题：1：定义api,2:导入调用3：转换成相应数据格式
    完成轮播图功能：
    1：使用插槽完成一个自定义处理，不影响以前功能
    2：使用自定义插槽完成内容渲染
     -->
    <XtxCarousel :sliders="newArr">
      <template v-slot="{ item }">
        <div class="slider">
          <GoodsItem v-for="i in item" :key="i.id" :goods="i"></GoodsItem>
        </div>
      </template>
    </XtxCarousel>
  </div>
</template>

<script>
import { goodsRelevant } from '@/api/product'
import { useRoute } from 'vue-router'
import { ref } from 'vue'
import GoodsItem from '@/views/category/components/goods-item.vue'
export default {
  // 同类推荐，猜你喜欢
  name: 'GoodsRelevant',
  components: {
    GoodsItem
  },
  setup () {
    const route = useRoute()
    const newArr = ref([])
    goodsRelevant(route.params.id).then(res => {
      while (res.result.length > 0) {
        newArr.value.push(res.result.splice(0, 4))
      }
      console.log('同类推荐', newArr)
    })
    return { newArr }
  }
}
</script>

<style scoped lang="less">
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
  .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: '';
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(@xtxColor, 40%);
      }
    }
  }
}
// 轮播商品
.slider {
  display: flex;
  justify-content: space-around;
  padding: 0 40px;
}
:deep(.xtx-carousel) {
  height: 380px;
  .carousel {
    &-indicator {
      bottom: 30px;
      span {
        &.active {
          background: @xtxColor;
        }
      }
    }
    &-btn {
      top: 110px;
      opacity: 1;
      background: rgba(0, 0, 0, 0);
      color: #ddd;
      i {
        font-size: 30px;
      }
    }
  }
}
</style>
